/*
 * @Author: your name
 * @Date: 2020-05-15 11:08:43
 * @LastEditTime: 2020-05-21 16:45:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\api\wbms\group.js
 */
var express = require('express');
var tag_group_router = express.Router();
var db = require('../../models');


tag_group_router.post('/groupList', async (req, res) => {
    let groups = await db.group.getGroupList()
    let data = {
        code: 200,
        rows: groups.rows,
        count:groups.count
    }
    res.json(data)
}),

    //分页获取分组

    tag_group_router.post('/groupTableList', async (req, res) => {
        var [page = 1, pagesize = 30] = [req.body.pageNo, req.body.pageSize]
        let condition = {
            "pagesize": pagesize,
            "page": page
        }
        let groups = await db.group.getTableGroupList(condition)
        let data = {
            code: 200,
            rows: groups
        }
        res.json(data)
    }),
    //获取分组总数
    tag_group_router.get('/groupCount', async (req, res) => {
        let groupCount = await db.group.getGroupCount()
        res.json(groupCount)
    }),
    /**
     * 修改分组，同时修改标签下所有分组对应信息  排除修改同名
     * @param {*} req 
     * @param {*} res 
     */
    tag_group_router.post('/editGroup', async (req, res) => {
        let result = {
            code: "",
            msg: "",
        }
        let queryData = { group_name: req.body.group_name }
        let canSave = await db.group.getOneGroup(queryData)
        if (canSave == null) {
            //排除同名修改  ->  修改分组表信息
            let data = await db.sequelize.transaction(t => {
                return db.group.update(
                    { group_name: req.body.group_name, },
                    {
                        'where': {
                            'code': req.body.code
                        },
                        transaction: t
                    },
                ).then(user => {
                    return db.tag.update({
                        group_name: req.body.group_name,
                    }, {
                        'where': {
                            'group': req.body.code
                        },
                        transaction: t
                    },
                    )
                });
            }).then(result => {
                return result
            }).catch(err => {
                return null
            })
            if (data) {
                result.code = "200"
                result.msg = "修改成功"
                res.json(result)
            }
        } else {
            result.code = "201"
            result.msg = "已存在分组，请更换分组名"
            res.json(result)
        }
    }),
    /**
     * 新增分组
     */
    tag_group_router.post('/saveGroup', async (req, res) => {
        let result = {
            code: "",
            msg: "",
        }
        let queryData = { group_name: req.body.group_name }
        let canSave = await db.group.getOneGroup(queryData)
        if (canSave == null) {
            let data = await db.group.create({
                group_name: req.body.group_name,
            }
            ).then(re => {
                return true
            }).catch(err => {
                return false
            })
            if (data) {
                result.code = "200"
                result.msg = "添加成功"
                res.json(result)
            }
        } else {
            result.code = "201"
            result.msg = "已存在分组，请勿重复添加"
            res.json(result)
        }
    }),
    /**
     * 删除分组
     */
    tag_group_router.post('/groupDel', async (req, res) => {
        let groupId = req.body.groupId
        //修改标签表的分组信息
        let result = await db.sequelize.transaction(t => {
            return db.tag.update(
                { group: 1, group_name: '其他' },
                {
                    where: {
                        "group": groupId
                    },
                    transaction: t
                },
            ).then(() => {
                return db.group.destroy(
                    {
                        where: {
                            "code": groupId
                        },
                        transaction: t
                    }).then(re=>{
                        return true
                    }).catch(err=>{
                        return false
                    })
            }).catch(err=>{
                return false
            })
        }).then(result => {
            return result
        }).catch(err => {
            return false
        })
        res.json(result > 0)
    })




module.exports = tag_group_router
